// Package models 模型通用属性和方法
package cm

import (
	"encoding/json"
	"github.com/spf13/cast"
)

// BaseModel 模型基类
type BaseModelId struct {
	ID uint64 `gorm:"column:id;primaryKey;autoIncrement;" json:"id,omitempty" mapstructure:"id"`
}

type BaseModelFiled struct {
	BaseFileds string `default:"*"`
	Page       int    `default:"0"`
	PageSize   int    `default:"20"`
	OrderBy    string
	WhereSql   string
	WhereData  []byte //map[string]interface 转的 json
}

func (obj *BaseModelFiled) GetSelectFile() string {
	if obj.BaseFileds == "" {
		return "*"
	}
	return obj.BaseFileds
}

func (obj *BaseModelFiled) GetPage() string {
	if obj.BaseFileds == "" {
		return "*"
	}
	return obj.BaseFileds
}

func (obj *BaseModelFiled) GetPageSize() string {
	if obj.BaseFileds == "" {
		return "*"
	}
	return obj.BaseFileds
}

func (obj *BaseModelFiled) SetSelectFile(field string) {
	obj.BaseFileds = field
}

func (obj *BaseModelFiled) SetOrderBy(orderBy string) {
	obj.OrderBy = orderBy
}

func (obj *BaseModelFiled) SetInput(input map[string]interface{}) {

	if _, ok := input["page"]; ok == true {
		obj.Page = cast.ToInt(input["page"])
	}

	if _, ok := input["page_size"]; ok == true {
		obj.PageSize = cast.ToInt(input["page_size"])
	}

	if _, ok := input["where_sql"]; ok == true {
		obj.WhereSql = cast.ToString(input["where_sql"])
	}

	if jsonStr, err := json.Marshal(input); err != nil {
		obj.WhereData = jsonStr
	}
}
